//
// Created by lwj12 on 24-7-29.
//
#include "Queue.h"

void QueueInit(Queue* q)
{
    assert(q);
    q->phead = q->ptail = NULL;
    q->sz = 0;
}

void QueuePush(Queue* q, DataType val)
{
    assert(q);
    QueueNode* tmp = (QueueNode*)malloc(sizeof(QueueNode));
    if(tmp == NULL)
    {
        perror("QueuePush malloc fail");
        return;
    }
    tmp->val = val;
    tmp->next = NULL;
    if(q->sz == 0)
        q->phead = q->ptail = tmp;
    else
    {
        q->ptail->next = tmp;
        q->ptail = tmp;
    }
    q->sz++;
}

void QueuePop(Queue* q)
{
    assert(q);
    assert(q->sz);
    QueueNode* tmp = q->phead->next;
    free(q->phead);
    q->phead =tmp;
    if(tmp == NULL) q->ptail = NULL;
    q->sz--;
}

void QueueDestroy(Queue* q)
{
    assert(q);
    while (q->sz != 0) QueuePop(q);
}
